<?php

namespace Plugins\AdminBasic\Http\Admin\Service\MakeCURDFiles;

use Plugins\AdminBasic\Http\Admin\Service\MakeCURDService;
use JetBrains\PhpStorm\ExpectedValues;

/**
 * @property-read $files
 * @property-read $controllerMethod
 * @property-read $table
 * @property-read $tableComment
 * @property-read $dir
 * @property-read $viewType
 * @property-read $windowType
 * @property-read $fields
 * @property-read $commonReplaceVar
 * @property-read $baseFileName
 * @property-read $isCover
 * @property-read $statusMapTranslationRes
 * @property-read $plugins
 * @property-read $isPlugins
 * @property-read $baseFileDir
 * @property-read $windowWidth
 * Class AbstractMake
 */
abstract class AbstractMake
{
    public function __construct(private readonly MakeCURDService $CURDService){}

    public function __get(string $name)
    {
        return $this->CURDService->attrGetter($name);
    }

    protected function getAddPrefixStr(string $str, string $prefix): string
    {
        return $this->CURDService->getAddPrefixStr($str, $prefix);
    }

    /**
     * @param array       $replaceVar
     * @param bool|string $fileContent
     *
     * @return array|string|string[]|null
     */
    protected function varHandle(array $replaceVar, bool|string $fileContent): string|array|null
    {
        $replaceVar = array_merge($this->commonReplaceVar, $replaceVar);

        return preg_replace_callback('/\{\{ (\w+) }}/', function ($match) use ($replaceVar) {
            return $replaceVar[$match[1]] ?? '';
        }, $fileContent);
    }

    /**
     * @param string $filename
     * @param string $fileContent
     *
     * @return string
     */
    protected function makeFile(string $filename, string $fileContent): string
    {
        $dir = dirname($filename);
        if (!is_dir($dir)){
            mkdir($dir, 0777, true);
        }

        if (file_exists($filename) && !$this->isCover) {
            return "文件已存在";
        }

        if (!file_put_contents($filename, $fileContent)){
            return "创建失败";
        }

        return "创建成功";
    }

    /**
     * @param string $Mode
     *
     * @return string
     */
    protected function getFilename(#[ExpectedValues(['Controller', 'Model', 'Service', 'FormRequest', 'Enum', 'View',])] string $Mode): string
    {
        return sprintf(MakeCURDService::getTemplate($this->baseFileDir[$Mode], $this->plugins), $this->baseFileName);
    }

    protected function getNamespace(#[ExpectedValues(['Models', 'Requests', 'Controller', 'Service', 'Enums'])] string $filetype): string
    {
        $dirs = $this->getAddPrefixStr(strtr($this->dir, ['/' => '\\']), '\\');

        if (in_array($filetype, ['Models', 'Requests', 'Enums'])) {
            return ($this->isPlugins ? "Plugins\\$this->plugins\\$filetype" : "App\\$filetype") . $dirs;
        }

        return ($this->isPlugins ? "Plugins\\$this->plugins\\Http\\Admin\\$filetype" : "App\\Http\\Admin\\$filetype") . $dirs;
    }

    abstract public function make(): string;
}